#encoding=utf8
'''
Created on 2012-9-21

@author: Administrator
'''
from Processor import Processor
from models.User import User
from models.UserModel import *
from pub import Func,UserDict
from pub.Config import Config
class URClass(Processor):
    '''
    指令：UR (user register)
    备注：用户注册
    输出格式：cmd #cmdID#userID#result#msg
    输出格式：UR#1#UserEmail#Password
    '''
    def __init__(self,):
        Processor.__init__(self,)
        self.__outdata=None
        self.__field = {}
    def dataValidtor(self,cmdContent):
        if(len(cmdContent)==2):
            self.__field["userName"]=cmdContent[0]
            self.__field["userPass"] = cmdContent[1]
            return True
        else:
            return False
    def Register(self,):
        '''
        用户登陆操作
        '''
        cUser = UserDict.getUserFromUserDictByUserName(self.__field["userName"])
        if(cUser):
            cUser.set_socket(self._sockObj);            
            result = (Config.I_SYS_ZERO,Config.I_USER_REGISTER_USERNAME_FOUND)
        else:
            mUser = UserModel()
            result = mUser.regNewUser(self.__field["userName"], self.__field["userPass"])
            
        return result   
    def outData(self):
        '''
        输出执行结果集
        '''
        (regId,regState) = self.Register()
        result = Config.I_SYS_ZERO
        if(regState==Config.I_USER_REGISTER_OK):
            result = Config.I_SYS_ONE
        Func.debugInfo(regState)
        Command = "%s#%s#%s#%s#%s"%\
        (self._cmd,self._cmdId,result,regId,regState)
        self._sockObj.sendLine(str(Command))
        return Config.I_SYS_ONE;